home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / rlib / tril.r < prev    next >
Text File  |  1994-04-25  |  836b  |  42 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    tril ( A )
  4. //        tril ( A , K )
  5.  
  6. //  Description:
  7.  
  8. //  tril(x) returns the lower triangular part of A.
  9.  
  10. //  tril(A,K) returns the elements on and below the K-th diagonal of
  11. //  A.
  12.  
  13. //  K = 0: main diagonal
  14. //  K > 0: above the main diag.
  15. //  K < 0: below the main diag.
  16.  
  17. //  See Also: triu
  18. //-------------------------------------------------------------------//
  19.  
  20. tril = function(x, k) 
  21. {
  22.   local(i, j, nr, nc, y);
  23.  
  24.   if (!exist (k)) { k = 0; }
  25.   nr = x.nr; nc = x.nc;
  26.   if(k > 0) 
  27.   { 
  28.     if (k > (nc - 1)) { error ("tril: invalid value for k"); }
  29.   else
  30.     if (abs (k) > (nr - 1)) { error ("tril: invalid value for k"); }
  31.   }
  32.  
  33.   y = zeros(nr, nc);
  34.  
  35.   for(i in max( [1,1-k] ):nr) {
  36.     j = 1:min( [nc, i+k] );
  37.     y[i;j] = x[i;j];
  38.   }
  39.  
  40.   return y;
  41. };
  42.